<?php
App::import('Model', 'Movimientosvalde');
$modeloMovimientosValde = new Movimientosvalde();
$sucursalUsuario = $this->Session->read('Auth.User.sucursale_id');
?>
<?php $rol = $this->Session->read('Auth.User.role'); ?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#grid').dataTable({
            "bJQueryUI": false,
            "bAutoWidth": false,
            "sPaginationType": "full_numbers",
            "sDom": '<"H"fl>t<"F"ip>',
            "aaSorting": [[0, "desc"]]
        });
    });
</script>
<?php foreach ($listadoValdes as $t): ?>
    <style>
        #element_to_pop_up_<?php echo $t['Valde']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none;
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }                   
    </style>

    <style>
        #element_to_pop_up_regulariza_<?php echo $t['Valde']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none;
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }
        .b-close{
            cursor:pointer;
            position:absolute;
            right:10px;
            top:5px;            
        }    
    </style>
<?php endforeach; ?>

<div class="widget">
    <div class="whead"><h6>LISTADO DE PRODUCTOS</h6><div class="clear"></div></div>
    <div class="shownpars">
        <table cellpadding="0" cellspacing="0" border="0" class="dTable">
            <thead>
                <tr>
                    <th>Nro</th>
                    <th>Nombre</th>
                    <th>Peso</th>
                    <th>Cantidad (Nuevos)</th>                                   
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php $i = 1; ?>
                <?php foreach ($valdes as $emp): ?>
                <div id="element_to_pop_up_<?php echo $emp['Valde']['id']; ?>">
                    <a class="b-close">X</a>                    
                    <div class="widget grid12">
                        <div class="whead"><h6>INGRESO AL ALMACEN </h6><div class="clear"></div></div>
                        &nbsp;
                        <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $emp['Valde']['nombre']; ?></h4>
                        <?php echo $this->Form->create('Movimientosvaldes', array('action' => 'insertar', 'id' => "validate")); ?>
                        <div class="formRow">                                                                                                                
                            <div class="grid3"><label>Cantidad:<span class="req">*</span></label></div>
                            <div class="grid3">
                                <?php echo $this->Form->text('cantidad', array('size' => 3, 'required', 'style' => 'width: 80px;')); ?>
                                <?php echo $this->Form->hidden('producto_id', array('value' => $emp['Valde']['id'])); ?>
                            </div>
                        </div>    
                        <div class="clear"></div>
                        <div class="formRow">                                                                                                                
                            <div class="grid3"><label>Prestado:<span class="req">*</span></label></div>
                            <div class="grid3">
                                <?php //echo $this->Form->text('cantidad', array('size' => 3, 'required')); ?>                    
                                <input type="checkbox" value="S" name="data[Movimientosvaldes][prestado]" onclick="muestraquien()">
                            </div>
                        </div>

                        <div class="formRow" id="mostrarquien">                                                                                                                
                            <div class="grid3"><label>Quien Presto:<span class="req">*</span></label></div>
                            <div class="grid3">
                                <?php echo $this->Form->text('quienpresto', array('size' => 3, 'style' => 'width: 150px;')); ?>                    
                            </div>
                        </div> 
                        <div class="formRow">
                            <input type="submit" value="Ingresar Valde" class="buttonS bGreen" /><div class="clear"></div>            
                        </div>
                        </form>
                    </div>
                </div> 

                <div id="element_to_pop_up_regulariza_<?php echo $emp['Valde']['id']; ?>">
                    <a class="b-close">X</a>                    
                    <div class="widget grid12">
                        <div class="whead"><h6>REGULARIZA PRODUCTO</h6><div class="clear"></div></div>
                        &nbsp;
                        <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $emp['Valde']['nombre']; ?></h4>
                        <?php echo $this->Form->create('Movimientosvaldes', array('action' => 'regulariza', 'id' => "validate")); ?>
                        <div class="formRow">                                                                                                                
                            <div class="grid3"><label>Codigo:<span class="req">*</span></label></div>
                            <div class="grid3">
                                <?php echo $this->Form->text('codigo', array('size' => 3, 'required')); ?>
                                <?php echo $this->Form->hidden('producto_id', array('value' => $emp['Valde']['id'])); ?>
                            </div>
                        </div>    
                        <div class="clear"></div>
                        <div class="formRow">                                                                                                                
                            <div class="grid3"><label>Peso:<span class="req">*</span></label></div>
                            <div class="grid3">
                                <?php echo $this->Form->text('peso', array('size' => 3, 'required')); ?>                                
                            </div>
                        </div>    
                        <div class="clear"></div>
                        <div class="formRow">
                            <input type="submit" value="Ingresar Valde" class="buttonS bGreen" /><div class="clear"></div>            
                        </div>
                        </form>
                    </div>
                </div> 

                <tr <?php echo fmod($i, 2) ? "class = bg" : ""; ?>>
                    <td><?php
                        echo $i;
                        $i++;
                        ?>
                    </td>
                    <td><?php echo $emp['Valde']['nombre']; ?></td>                                        
                    <td><?php echo $emp['Valde']['peso']; ?> Kilos</td>
                    <td>
                        <?php
                        //echo $emp['Valde']['peso'];
                        if ($sucursalUsuario == 6)
                        {
                            $cantidadActual = $modeloMovimientosValde->find('first', array(
                                'recursive' => -1,
                                'conditions' => array(
                                    'Movimientosvalde.valde_id' => $emp['Valde']['id'],
                                    'Movimientosvalde.sucursale_id' => 6,
                                ),
                                'order' => 'Movimientosvalde.id DESC'
                            ));
                            //debug($cantidadActual); 
                            if (!empty($cantidadActual))
                            {
                                echo $cantidadActual['Movimientosvalde']['cantidad'];
                            } else
                            {
                                echo '0';
                            }
                        } else
                        {
                            $cantidadValdesSucursal = $modeloMovimientosValde->find('count', array(
                                'conditions' => array('Movimientosvalde.sucursale_id' => $sucursalUsuario)
                            ));
                            echo $cantidadValdesSucursal;
                        }
                        ?> Unidades
                    </td>                                         
                    <td>
                        <?php echo $this->Html->link($this->Html->image('iconos/listaInv.png', array("title" => 'Mostrar Valdes')), array('controller' => 'Movimientosvaldes', 'action' => 'verdetalle', $emp['Valde']['id']), array('escape' => false)); ?>&nbsp;
                        <?php if ($rol == 'admin'): ?>
                            <a onclick="ajaxIngresa<?php echo $emp['Valde']['id']; ?>();"><?php echo $this->Html->image("iconos/add.png", array('title' => 'Ingreso Almacen')) ?></a>
                            <a onclick="ajaxRegulariza<?php echo $emp['Valde']['id']; ?>();"><?php echo $this->Html->image("iconos/regulariza.png", array('title' => 'Regulariza Producto')) ?></a>                                                          
                            <!--<a id="btAjaxGeneraHr_2<?php //echo $emp['Valde']['id'];        ?>"><?php //echo $this->Html->image("iconos/salida.png", array('title' => 'Salida Almacen'))        ?></a>-->                        
                            &nbsp;                        
                            <?php echo $this->Html->link($this->Html->image('iconos/edit.png', array("alt" => 'Editar', 'title' => 'Editar Producto')), array('action' => 'editar', $emp['Valde']['id']), array('escape' => false)); ?>&nbsp;                        
                            <?php 
                                $nombreValde = $emp['Valde']['nombre'];
                            ?>
                            <?php
                            echo $this->Form->postLink(
                                    $this->Html->image('iconos/elim.png', array(
                                        "title" => 'Eliminar Producto'
                                    )), array(
                                'action' => 'eliminar',
                                $emp['Valde']['id']), array(
                                'escape' => false,
                                'confirm' => "Esta seguro de Eliminar $nombreValde")
                            );
                            ?>
                        <?php else: ?>

                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>                
            </tbody>
        </table>
        <?php foreach ($valdes as $emp): ?>
            <script>
        function ajaxIngresa<?php echo $emp['Valde']['id']; ?>() {
            $('#element_to_pop_up_<?php echo $emp['Valde']['id']; ?>').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int 
                width: 20,
            });
        }

        function ajaxRegulariza<?php echo $emp['Valde']['id']; ?>() {
            $('#element_to_pop_up_regulariza_<?php echo $emp['Valde']['id']; ?>').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int 
                width: 20,
            });
        }
            </script>         
        <?php endforeach; ?>
        <?php if ($rol == 'admin'): ?>
            <?php
            echo $this->Html->link('INSERTAR NUEVO PRODUCTO', array('action' =>
                'insertar'), array('class' => 'buttonS bGreen'));
            ?>
        <?php else: ?>
        <?php endif; ?>
    </div> 
</div> 


